package stool.iteratorpattern;

/**
 * @Author Felix YF Dong
 * @Date 2021/5/11
 */
public class ConcreteIteratorDesc extends Iterator{

    private ConcreteAggregate concreteAggregate;

    private int current = 0;

    public ConcreteIteratorDesc(ConcreteAggregate concreteAggregate) {
        this.concreteAggregate = concreteAggregate;
        current = concreteAggregate.size() - 1;
    }

    @Override
    public Object first() {
        return concreteAggregate.get(concreteAggregate.size() - 1);
    }

    @Override
    public Object next() {

        Object obj = null;
        current --;
        if (current >= 0) {
            obj = concreteAggregate.get(current);
        }

        return obj;
    }

    @Override
    public boolean isDone() {
        return current < 0;
    }

    @Override
    public Object current() {
        return concreteAggregate.get(current);
    }
}
